home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1996 July / C-C++ Users Group Library July 1996.iso / vol_200 / 227_01 / graftext.c < prev    next >
Text File  |  1988-02-07  |  10KB  |  270 lines

  1. /*
  2.  * g r a f t e x t . c
  3.  * -------------------
  4.  * This is a hardware-independant, high-level part of the compatible
  5.  * graphics library.
  6.  * This module implements the text-handling functions.
  7.  * The standard font is defined in "stdfont.h".
  8.  *
  9.  * release history
  10.  * ---------------
  11.  * May, 28. 1987    Begin development
  12.  * Jun, 17. 1987    As support to the graphics library error system added
  13.  *                  function "pgrerror", which is like perror but prints
  14.  *                  errors occuring in the graphics library. It is included
  15.  *                  in this module, because it needs text output functions.
  16.  * Sep, 13. 1987    Implemention of the different write modes.
  17.  *
  18.  * Written by       Rainer Gerhards
  19.  *                  Petronellastr. 6
  20.  *                  D-5112 Baesweiler
  21.  *                  West Germany
  22.  *                  Phone (49) 2401 - 1601
  23.  */
  24.  
  25. #include <stdio.h>
  26. #include <string.h>
  27. #include <dos.h>
  28. #include <math.h>
  29.  
  30. #include "graphics.h"
  31. #include "stdfont.h"
  32.  
  33. #ifdef    USEPROTT
  34.     extern void    fputmsg(int, int, int, int, char *, int, int);
  35. #endif
  36.  
  37. /*
  38.  *    auxiluary function set for use with write modes other than WM_NORM
  39.  */
  40.  
  41. /*
  42.  * name          fputmsg
  43.  *
  44.  * synopsis      putmsg(x, y, foreg, backg, string, font, mode);
  45.  *               int x, y;            starting coordinate (upper left corner)
  46.  *               int foreg;           foreground color
  47.  *               int backg;           background color
  48.  *               char *string;        string to draw
  49.  *               int font;            font-number to use
  50.  *               int mode;            output mode (font modifier)
  51.  *
  52.  */
  53. static void fputmsg(x, y, foreg, backg, string, font, mode)
  54. int x,y;
  55. int foreg;
  56. int backg;
  57. char *string;
  58. int font;
  59. int mode;
  60. {
  61. #define chrwidth 8 /* character width, STDFONT = 8 pixel  */
  62. #define chrlen   8 /* character length, STDFONT = 8 pixel */
  63.  
  64. register unsigned bitmask;        /* to mask current font-bit    */
  65. register int xt;            /* x-offset in character    */
  66. register int yt;            /* y-offset in character    */
  67. register int modemodi;            /* x-modifier for selected mode    */
  68. register int cx, cy;            /* current x, y            */
  69.  
  70. for( ; *string ; string++, x += chrwidth)    /* process string    */
  71.     for(yt = 0 ; yt < chrlen ; ++yt)        /* one char. line    */
  72.       for(  cy = y + yt, xt = chrwidth, bitmask = 0x01    /* pixel in this line    */
  73.           ; xt >= 0
  74.           ; --xt, bitmask <<= 1)
  75.         {
  76.         switch(mode)
  77.           {
  78.           case OM_ITAL : /* italics */
  79.                          modemodi = 8 - yt;
  80.                          break;
  81.           case OM_RITAL: /* reverse italics */
  82.                          modemodi = yt;
  83.                          break;
  84.           default      : /* if mode is unknown, use OM_NORM */
  85.                          modemodi = 0;
  86.                          break;
  87.           }
  88.     curcolor = (stdchrst[(*string << 3) + yt] & bitmask) ? foreg : backg;
  89.     cx = x + xt + modemodi;
  90.         setpixel(cx, cy, selcolor(cx, cy));
  91.         }
  92. }
  93.  
  94.  
  95. /*
  96.  *    main function set, called by user program
  97.  */
  98.  
  99.  
  100. /*
  101.  * name          putmsg
  102.  *
  103.  * synopsis      putmsg(x, y, foreg, backg, string, font, mode);
  104.  *               int x, y;            starting coordinate (upper left corner)
  105.  *               int foreg;           foreground color
  106.  *               int backg;           background color
  107.  *               char *string;        string to draw
  108.  *               int font;            font-number to use
  109.  *               int mode;            output mode (font modifier)
  110.  *
  111.  * description   This function draws the string pointed to by "string" on the
  112.  *               graphics screen. The string must be \0-terminated. Drawing
  113.  *               begins at x,y, which is the upper left corner of the first
  114.  *               character in the string. The characters are painted in color
  115.  *               foreg, the character-background is painted in color backg.
  116.  *               The font to use must be specified in font. Currently only
  117.  *               STDFONT is supported. Field mode contains the string output
  118.  *               mode, which is a font modifier. This field may be used to
  119.  *               select italics or other output modes. Currently only
  120.  *               OM_NORM and OM_ITAL are supported.
  121.  *
  122.  * warning     Although this function supports the write mode system, only
  123.  *         very little write modes makes sense. In inverting mode, e. q.
  124.  *         the whole character is inverted and as thus invisible!
  125.  *         I think you should switch to WM_NORM before doing any text-io.
  126.  */
  127. #ifdef USEVOID
  128. void
  129. #endif
  130. putmsg(x, y, foreg, backg, string, font, mode)
  131. int x,y;
  132. int foreg;
  133. int backg;
  134. char *string;
  135. int font;
  136. int mode;
  137. {
  138. #define chrwidth 8 /* character width, STDFONT = 8 pixel  */
  139. #define chrlen   8 /* character length, STDFONT = 8 pixel */
  140.  
  141. register unsigned bitmask;        /* to mask current font-bit */
  142. register int xt;                  /* x-offset in character */
  143. register int yt;                  /* y-offset in character */
  144. register int modemodi;            /* x-modifier for selected mode */
  145.  
  146. if(writemod != WM_NORM)
  147.   fputmsg(x, y, foreg, backg, string, font, mode);
  148. else
  149.   {
  150.   #ifdef EGAGRAF
  151.   setewm2();
  152.   #endif
  153.   #ifdef HERCGRAF
  154.   foreg = foreg & 1;
  155.   #endif
  156.   for( ; *string ; string++, x += chrwidth)    /* process string */
  157.     #if (HERCGRAF || EGAGRAF) /* && */
  158.     if((mode == OM_NORM) && (chrwidth == 8) && !(x & 0x0007))
  159.       for(yt = 0 ; yt < chrlen ; ++yt)
  160.     #if EGAGRAF
  161.       {
  162.       /* I now overwrite the wohle character line with the
  163.        * background color, and then write the character line itself
  164.        * in foreground color. You may also clear the background with
  165.        * only (~charcter_line), but there seems to be no speed
  166.        * difference. So I use the first method, because it needs one
  167.        * word less stack-space (pick up character line only once!).
  168.        */
  169.       setbyte(x, y + yt, backg, 0xff);
  170.       setbyte(x, y + yt, foreg, stdchrst[(*string << 3) + yt]);
  171.       }
  172.     #else
  173.       setbyte(x, y + yt, foreg ? stdchrst[(*string << 3) + yt]
  174.                    : ~stdchrst[(*string << 3) + yt]);
  175.     #endif
  176.     else
  177.     #endif
  178.       for(yt = 0 ; yt < chrlen ; yt++)           /* one char. line */
  179.     for(  xt = chrwidth, bitmask = 0x01      /* pixel in this line */
  180.         ; xt >= 0
  181.         ; --xt, bitmask <<= 1)
  182.       {
  183.       switch(mode)
  184.         {
  185.         case OM_ITAL : /* italics */
  186.                modemodi = 8 - yt;
  187.                break;
  188.         case OM_RITAL: /* reverse italics */
  189.                modemodi = yt;
  190.                break;
  191.         default      : /* if mode is unknown, use OM_NORM */
  192.                modemodi = 0;
  193.                break;
  194.         }
  195.       #ifdef EGAGRAF   /* select set-pixel function */
  196.       qsetpix
  197.       #else
  198.       setpixel
  199.       #endif
  200.           (x + xt + modemodi, y + yt,
  201.           (stdchrst[(*string << 3) + yt] & bitmask) ? foreg : backg);
  202.       }
  203.   #ifdef EGAGRAF
  204.   rsestdwm();
  205.   #endif
  206.   }
  207. }
  208.  
  209.  
  210. #ifdef USEVOID
  211. void
  212. #endif
  213. grperror(fil, msg)
  214. FILE *fil;
  215. char *msg;
  216. /*
  217.  * name          grperror
  218.  *
  219.  * synopsis      grperror(fil);
  220.  *               FILE *fil;      file to put error-message in (or NULL if to
  221.  *                               put on current display).
  222.  *               char *msg;      error-message header text (like perror).
  223.  *
  224.  * description   This function prints the last detected error that occured
  225.  *               in the graphics library.
  226.  *               It has the ablety to write to a user text file or to the
  227.  *               current graphics screen. Writing to a file may be usefull
  228.  *               for not destroying your graphics display. You chose between
  229.  *               this both modes by your file pointer: if it is NULL, the
  230.  *               function writes to the display, otherwise it uses your file
  231.  *               pointer to write to that file.
  232.  *               No write errors are reported.
  233.  *               The function writes first the user-specified message, than
  234.  *               the internal error-message.
  235.  */
  236. {
  237. register int nxtpos;      /* next output position */
  238. register int errind;      /* error index, used to index err_msgs[] */
  239. static char *err_msgs[] = {
  240.